import java.util.HashMap;

/**
* @description: 911. 在线选举
* @author hewei
* @date 2022/9/16 16:35
* @version 1.0
*/

public class TopVotedCandidate {

    int[] count;
    int[] time;
    public TopVotedCandidate(int[] persons, int[] times) {
        count = new int[times.length];
        this.time = times;
        HashMap<Integer, Integer> map = new HashMap<>();
        int max = -1;
        for (int i = 0; i < persons.length; i++) {
            map.put(persons[i], map.getOrDefault(persons[i], 0) + 1);
            Integer temp = map.get(persons[i]);
            if (max == -1 || map.get(max) <= temp) max = persons[i];
            count[i] = max;
        }
    }

    public int q(int t) {
        int l = 0;
        int r = time.length - 1;
        while (l < r) {
            int mid = (r - l) / 2 + l + 1;
            if (t >= time[mid]) {
                l = mid;
            } else r = mid - 1;
        }
        return count[l];
    }
}